package com.dudsbury.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.border.BevelBorder;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import com.dudsbury.database.DatabaseExtension;
import com.dudsbury.database.MyCustomer;
import com.dudsbury.database.MyItem;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class CustomerScreen extends JFrame {

	CustomerScreen thisFrame = this;
	private JPanel contentPane;
	private JTextField customerAddress2;
	private JTextField customerPostcode;
	private JTextField customerEmail;
	private JTextField customerSurname;
	private JTextField customerForname;
	private JTextField customerAddress1;
	private JTextField customerTelephone;
	private JTextField customerCounty;
	private JTextField customerTown;
	private JComboBox customerInfoCombo;
	private JComboBox<String> customerTitleCombo;
	private String id;
	ButtonGroup group;
	// Static variables for Title radio buttons
	public static final int MR = 0;
	public static final int MRS = 1;
	public static final int MISS = 2;
	public static final int MS = 3;

	// Static variables for "How did you hear from us" combo box

	public static final int INTERNET = 0;
	public static final int NEWSPAPER = 1;
	public static final int FRIEND = 2;
	public static final int OTHER = 3;

	MyCustomer customer;
	SearchScreen screen;
	public CustomerScreen(MyCustomer customer,SearchScreen screen) {
		this();
		this.customer=customer;
		id = customer.getCustomerID();
		
		this.screen=screen;
			
			
			System.out.println("Index : "  + Integer.valueOf(customer.getTitleType()));
	customerTitleCombo.setSelectedIndex(Integer.valueOf(customer.getTitleType()));
		
		
		customerForname.setText(customer.getForename());
		customerSurname.setText(customer.getSurname());
		customerPostcode.setText(customer.getPostcode());
		customerAddress1.setText(customer.getAddress1());
		customerAddress2.setText(customer.getAddress2());
		customerTown.setText(customer.getTownOrCity());
		customerCounty.setText(customer.getCounty());
		customerTelephone.setText(customer.getTelephoneNo());
		customerEmail.setText(customer.getEmail());
		
		customerInfoCombo.setSelectedIndex(Integer.valueOf(customer.getInfoReferenceType()));
		
	
		//group = new ButtonGroup();

		
		
	}

	/**
	 * Create the frame.
	 */
	public CustomerScreen() {

		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 463, 774);

		setTitle("New Customer");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 695, 435);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JLabel label = new JLabel("Forename:");

		JLabel label_1 = new JLabel("Surname:");

		customerSurname = new JTextField();
		customerSurname.setText((String) null);
		customerSurname.setColumns(10);

		customerForname = new JTextField();
		customerForname.setText((String) null);
		customerForname.setColumns(10);

		JLabel label_9 = new JLabel("Title:");

		JLabel label_13 = new JLabel("Customer Details");
		label_13.setHorizontalAlignment(SwingConstants.CENTER);
		label_13.setFont(new Font("Tahoma", Font.BOLD, 11));

		customerTitleCombo = new JComboBox();
		customerTitleCombo.setModel(new DefaultComboBoxModel(new String[] {
				"Mr", "Mrs", "Miss", "Ms" }));
		// updateComboBox.setModel(new DefaultComboBoxModel();
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1
				.setHorizontalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.TRAILING)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				label_13,
																				GroupLayout.DEFAULT_SIZE,
																				393,
																				Short.MAX_VALUE)
																		.addContainerGap())
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addGroup(
																				gl_panel_1
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								label_1)
																						.addComponent(
																								label_9)
																						.addComponent(
																								label))
																		.addGap(45)
																		.addGroup(
																				gl_panel_1
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addGroup(
																								gl_panel_1
																										.createSequentialGroup()
																										.addComponent(
																												customerTitleCombo,
																												GroupLayout.PREFERRED_SIZE,
																												66,
																												GroupLayout.PREFERRED_SIZE)
																										.addContainerGap())
																						.addGroup(
																								gl_panel_1
																										.createSequentialGroup()
																										.addGroup(
																												gl_panel_1
																														.createParallelGroup(
																																Alignment.TRAILING,
																																false)
																														.addComponent(
																																customerSurname,
																																Alignment.LEADING)
																														.addComponent(
																																customerForname,
																																Alignment.LEADING,
																																GroupLayout.DEFAULT_SIZE,
																																153,
																																Short.MAX_VALUE))
																										.addContainerGap()))))));
		gl_panel_1
				.setVerticalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(label_13)
										.addGap(18)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																customerTitleCombo,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(label_9))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																customerForname,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(label))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_1)
														.addComponent(
																customerSurname,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(348, Short.MAX_VALUE)));
		panel_1.setLayout(gl_panel_1);

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JLabel label_11 = new JLabel(
				"How did you hear about Dudsbury Golf Club?:");
		label_11.setHorizontalAlignment(SwingConstants.CENTER);
		label_11.setFont(new Font("Tahoma", Font.BOLD, 11));

		customerInfoCombo = new JComboBox();
		customerInfoCombo.setModel(new DefaultComboBoxModel(new String[] {
				"Internet", "Newspaper", "Friend", "Other" }));
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3
				.setHorizontalGroup(gl_panel_3
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_3
										.createSequentialGroup()
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_3
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				label_11))
														.addGroup(
																gl_panel_3
																		.createSequentialGroup()
																		.addGap(34)
																		.addComponent(
																				customerInfoCombo,
																				GroupLayout.PREFERRED_SIZE,
																				194,
																				GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(10, Short.MAX_VALUE)));
		gl_panel_3.setVerticalGroup(gl_panel_3.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel_3
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(label_11)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(customerInfoCombo,
								GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(18, Short.MAX_VALUE)));
		panel_3.setLayout(gl_panel_3);

		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JButton customerSaveButton = new JButton("Save");
		customerSaveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				if (customerForname.getText().trim().length()<1 || customerSurname.getText().trim().length()<1) {
					JOptionPane.showMessageDialog(null, "Please Enter a Forename and Surname");
					return;
				}
				String[] data = new String[12];

				try {
					data[0] = customer.getCustomerID();
				} catch (Exception e) {
					data[0] = null;
				}
				data[1] = String.valueOf(customerTitleCombo.getSelectedIndex());
				data[2] = customerForname.getText();
				data[3] = customerSurname.getText();
				data[4] = customerPostcode.getText();
				data[5] = customerAddress1.getText();
				data[6] = customerAddress2.getText();
				data[7] = customerTown.getText();
				data[8] = customerCounty.getText();
				data[9] = customerTelephone.getText();
				data[10] = customerEmail.getText();
				data[11] = String.valueOf(customerInfoCombo.getSelectedIndex());

				
				DatabaseExtension.saveCustomer(new MyCustomer(data));
				if (screen!=null)
				screen.search();
				thisFrame.dispose();
			}
		});

		JButton customerClearButton = new JButton("Clear");
		customerClearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				customerForname.setText("");
				customerSurname.setText("");
				customerPostcode.setText("");
				customerAddress1.setText("");
				customerAddress2.setText("");
				customerTown.setText("");
				customerCounty.setText("");
				customerTelephone.setText("");
				customerEmail.setText("");
			}
		});

		JButton customerExitButton = new JButton("Exit");
		customerExitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				thisFrame.dispose();
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.TRAILING,
																false)
														.addComponent(
																panel,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								panel_1,
																								GroupLayout.PREFERRED_SIZE,
																								275,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								panel_3,
																								0,
																								0,
																								Short.MAX_VALUE))
																		.addGap(18)
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.LEADING,
																								false)
																						.addComponent(
																								panel_4,
																								GroupLayout.PREFERRED_SIZE,
																								355,
																								GroupLayout.PREFERRED_SIZE)
																						.addGroup(
																								gl_contentPane
																										.createSequentialGroup()
																										.addGap(10)
																										.addComponent(
																												customerSaveButton,
																												GroupLayout.PREFERRED_SIZE,
																												72,
																												GroupLayout.PREFERRED_SIZE)
																										.addGap(62)
																										.addComponent(
																												customerClearButton,
																												GroupLayout.PREFERRED_SIZE,
																												69,
																												GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												ComponentPlacement.RELATED,
																												GroupLayout.DEFAULT_SIZE,
																												Short.MAX_VALUE)
																										.addComponent(
																												customerExitButton,
																												GroupLayout.PREFERRED_SIZE,
																												62,
																												GroupLayout.PREFERRED_SIZE)
																										.addGap(11)))))
										.addContainerGap(40, Short.MAX_VALUE)));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(panel,
												GroupLayout.PREFERRED_SIZE, 61,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				panel_1,
																				GroupLayout.PREFERRED_SIZE,
																				140,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				panel_3,
																				GroupLayout.PREFERRED_SIZE,
																				78,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(66))
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				panel_4,
																				GroupLayout.PREFERRED_SIZE,
																				264,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.UNRELATED)
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.BASELINE)
																						.addComponent(
																								customerSaveButton)
																						.addComponent(
																								customerExitButton)
																						.addComponent(
																								customerClearButton))
																		.addGap(12)))
										.addGap(333)));

		JLabel label_10 = new JLabel("Contact Details");
		label_10.setHorizontalAlignment(SwingConstants.CENTER);
		label_10.setFont(new Font("Tahoma", Font.BOLD, 11));

		JLabel label_2 = new JLabel("Postcode:");

		customerPostcode = new JTextField();
		customerPostcode.setText((String) null);
		customerPostcode.setColumns(10);

		JLabel label_3 = new JLabel("Address 1:");

		customerAddress1 = new JTextField();
		customerAddress1.setText((String) null);
		customerAddress1.setColumns(10);

		JLabel label_4 = new JLabel("Address 2:");

		customerAddress2 = new JTextField();
		customerAddress2.setText((String) null);
		customerAddress2.setColumns(10);

		JLabel label_5 = new JLabel("Town/City:");

		customerTown = new JTextField();
		customerTown.setText((String) null);
		customerTown.setColumns(10);

		JLabel label_6 = new JLabel("County:");

		customerCounty = new JTextField();
		customerCounty.setText((String) null);
		customerCounty.setColumns(10);

		JLabel label_7 = new JLabel("Telephone No:");

		customerTelephone = new JTextField();
		customerTelephone.setText((String) null);
		customerTelephone.setColumns(10);

		JLabel label_8 = new JLabel("Email:");

		customerEmail = new JTextField();
		customerEmail.setText((String) null);
		customerEmail.setColumns(10);
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4
				.setHorizontalGroup(gl_panel_4
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_4
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_4
																		.createSequentialGroup()
																		.addGroup(
																				gl_panel_4
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								label_7)
																						.addComponent(
																								label_2)
																						.addComponent(
																								label_3)
																						.addComponent(
																								label_4)
																						.addComponent(
																								label_5)
																						.addComponent(
																								label_6)
																						.addComponent(
																								label_8))
																		.addGap(18)
																		.addGroup(
																				gl_panel_4
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								customerPostcode,
																								GroupLayout.PREFERRED_SIZE,
																								116,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								customerAddress1,
																								GroupLayout.DEFAULT_SIZE,
																								209,
																								Short.MAX_VALUE)
																						.addComponent(
																								customerAddress2,
																								GroupLayout.DEFAULT_SIZE,
																								209,
																								Short.MAX_VALUE)
																						.addGroup(
																								gl_panel_4
																										.createParallelGroup(
																												Alignment.TRAILING,
																												false)
																										.addComponent(
																												customerTelephone,
																												Alignment.LEADING)
																										.addComponent(
																												customerCounty,
																												Alignment.LEADING)
																										.addComponent(
																												customerTown,
																												Alignment.LEADING,
																												GroupLayout.DEFAULT_SIZE,
																												153,
																												Short.MAX_VALUE))
																						.addComponent(
																								customerEmail,
																								GroupLayout.DEFAULT_SIZE,
																								243,
																								Short.MAX_VALUE)))
														.addComponent(
																label_10,
																Alignment.TRAILING,
																GroupLayout.DEFAULT_SIZE,
																331,
																Short.MAX_VALUE))
										.addContainerGap()));
		gl_panel_4
				.setVerticalGroup(gl_panel_4
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_4
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(label_10)
										.addGap(11)
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_2)
														.addComponent(
																customerPostcode,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_3)
														.addComponent(
																customerAddress1,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_4)
														.addComponent(
																customerAddress2,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_5)
														.addComponent(
																customerTown,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_6)
														.addComponent(
																customerCounty,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_7)
														.addComponent(
																customerTelephone,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_8)
														.addComponent(
																customerEmail,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(24, Short.MAX_VALUE)));
		panel_4.setLayout(gl_panel_4);

		JLabel lblCu = new JLabel("New Customer");
		lblCu.setHorizontalAlignment(SwingConstants.CENTER);
		lblCu.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblCu, GroupLayout.DEFAULT_SIZE, 393,
								Short.MAX_VALUE).addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblCu, GroupLayout.DEFAULT_SIZE, 35,
								Short.MAX_VALUE).addContainerGap()));
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}
}
